require "import"
import "tools"

lay=
{
  LinearLayout;
  layout_width="-1";
  orientation="vertical";
  layout_height="-1";
  {
    LinearLayout;
    layout_width="-1";
    gravity="center|left";
    layout_height="60dp";
    background="#ff4DB6AC";
    {
      TextView;
      gravity="center";
      textSize="30sp";
      textColor="#ff000000";
      layout_marginLeft="20dp";
      text="已完成的文件";
      layout_height="-1";
    };
  };
  {
    PullingLayout;
    PullDownEnabled=true;
    id="pull";
    {
      ListView;
      layout_width="-1";
      id="lv";
      layout_height="-1";
    };
  };
};
activity.setContentView(loadlayout(lay))
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS).setStatusBarColor(0xff4DB6AC)

item={
  LinearLayout;
  layout_height="50dp";
  orientation="vertical";
  layout_width="100%w";
  {
    TextView;
    gravity="center|left";
    id="filename";
    layout_marginLeft="50dp";
    layout_height="-1";
    layout_width="-1";
  };
};
adp=LuaAdapter(activity,item)
lv. Adapter=adp
function refreah()
  adp.clear()
  local f=luajava.astable(File(LUA_SD.."/avHandle/output/").listFiles())
  for k,v in pairs(f) do
    adp.add{filename=File(tostring(v)).getName()}
  end
pull.refreshFinish(0)
end
pull.onRefresh=function()
  refreah()
end
lv. onItemClick=function(a,b)
  local items={"预览","分享","删除"}
  AlertDialog.Builder(this) .setTitle("请选择") .setItems(items,{onClick=function(l,v)
      local s=items[v+1]
      if s=="预览"
        local Mime=MimeTypeMap.getSingleton().getMimeTypeFromExtension( tostring(File( LUA_SD.."/avHandle/output/"..b.Tag.filename.text).Name):match("%.(.+)"))
        activity.startActivity( Intent() .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .setAction(Intent.ACTION_VIEW) .setDataAndType(Uri.fromFile(File( LUA_SD.."/avHandle/output/"..b.Tag.filename.text)), Mime))
      end
      if s=="分享"
        activity.startActivity(Intent.createChooser(Intent() .setAction(Intent.ACTION_SEND) .setType(MimeTypeMap.getSingleton().getMimeTypeFromExtension(tostring(File(LUA_SD.."/avHandle/output/"..b.Tag.filename.text).Name):match("%.(.+)"))) .putExtra(Intent.EXTRA_STREAM,Uri.fromFile(File(LUA_SD.."/avHandle/output/"..b.Tag.filename.text))) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), "分享到:"))
      end
      if s=="删除"
        os.remove(LUA_SD.."/avHandle/output/"..b.Tag.filename.text)
        refreah()
      end
  end}).show()
end
refreah()